// pages/unscramble/unscramble.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
		/* 解读列表查询id */
		parentId:"",
		unscrambleList:[],
		/* 页码 */
		page:1,
		/* 底线展示 */
		noneData:false,
		/* 手机屏幕高 */
		phoneHeight:0,
		/* 机器人移动的位置 */
		leftLooks: 280,
		topLooks: 350,
		/* 是否下拉刷新 */
		refresh:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		this.data.parentId=options.parentId;
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		/* 获取手机可视区域宽高 */
		var _this=this;
		wx.getSystemInfo({
			success: function(res) {
				_this.phoneHeight=res.windowHeight;
				_this.setData({
					phoneHeight:res.windowHeight,
					newHeight:res.windowHeight
				})
			},
		});
		this.unscrambleFn();
  },
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=wx.getStorageSync("phoneWidth");
		var phoneHeight=wx.getStorageSync("phoneHeight");
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
		this.setData({
			leftLooks: e.touches[0].clientX,
			topLooks: e.touches[0].clientY
		})
	},
	/* 跳转到详情页 */
	toDetails:function(e){
		wx.navigateTo({
			url: "../articleDetail/articleDetail?judgeType=manuscript&id="+e.currentTarget.dataset.id+"&parentId="+this.data.parentId
		})
	},
	/* 获取解读列表 */
	unscrambleFn:function(){
		var _this=this;
		wx.request({
			url: util.url+"/article/queryArticle",
			method:"post",
			data: {
				parentId:_this.data.parentId,
				page:_this.data.page,
				size:20,
				"orderBy":"file_date",
			},
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					if(_this.data.refresh){
						wx.hideNavigationBarLoading() //完成停止加载
						wx.stopPullDownRefresh() //停止下拉刷新
					}
					var articleList=res.data.resultData.articleList;
					var len=_this.data.unscrambleList.length;
					for(var i=0;i<articleList.length;i++){
						articleList[i].createAt=util.formatTime(new Date(articleList[i].createAt));
						articleList[i].fileDate=util.formatTime(new Date(articleList[i].fileDate));
						_this.data.unscrambleList[len+i]=articleList[i];
					}
					if(articleList.length){
						_this.setData({
							unscrambleList:_this.data.unscrambleList,
							page:_this.data.page++,
							noneData:false
						})
					}else{
						_this.setData({
							noneData:true
						})
						wx.createSelectorQuery().selectAll('.none-data').boundingClientRect(function (rect) {
							_this.setData({
								newHeight:_this.data.phoneHeight-rect[0].height
							})
						}).exec();
					}
					_this.data.refresh=false;
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		});
	},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		if(!this.data.noneData){
			this.unscrambleFn()
		}
  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
  	this.data.refresh=true;
  	this.data.page=1;
		this.data.unscrambleList=[];
  	this.unscrambleFn();
  	wx.showNavigationBarLoading() //在标题栏中显示加载
  },
	/**
		 * 用户点击右上角分享朋友
		 */
		onShareAppMessage: function (e) {
			
		},
		/**
		* 用户点击右上角分享朋友圈
		*/
		onShareTimeline: function () {
	
		 },
})